//
//  YMLeaseSectionHeaderView.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/16.
//  Copyright © 2018年 youmy. All rights reserved.
//

import UIKit
private let kCellId = "kCellId"
class YMLeaseSectionHeaderView: UIView {

    @IBOutlet weak var collectionView: UICollectionView!
    
    @IBOutlet weak var titleView: UIView!
    
    var titles = ["全新热门机型","9新特价抢购","租满即送"]
    
    var ym_titleView:YMPageTitleView?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.frame = ym_screen_bounds
        
        let frame = CGRect(x: 0, y: 0, width: ym_screen_width, height: 60)
        ym_titleView = YMPageTitleView(frame: frame, titles: titles)
        ym_titleView?.delegate = self
        titleView.addSubview(ym_titleView!)
        
        autoresizingMask = UIViewAutoresizing()
        collectionView.register(UINib.init(nibName: "YMLeaseNormalCell", bundle: nil), forCellWithReuseIdentifier: kCellId)
        collectionView.dataSource = self
        collectionView.delegate = self
        
        let kMargin:CGFloat = 1
        let kWidth:CGFloat = (ym_screen_width - kMargin * 1)/2
        let kHeihgt:CGFloat = 190
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: kWidth, height: kHeihgt)
        layout.minimumLineSpacing = kMargin
        layout.minimumInteritemSpacing = kMargin
        layout.sectionInset = UIEdgeInsetsMake(0, 0, kMargin, 0)
        
        refresh()
    }

}

// MARK: - 下拉、上拉刷新
extension YMLeaseSectionHeaderView {
    func refresh(){
        
//        collectionView?.mj_header = YMRefresh.headerRefresh(self, #selector(self.lonaNew))
        
        collectionView?.mj_footer = YMRefresh.footerRefresh(self, #selector(self.loadMore))
    }
    
    @objc func lonaNew(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
            self.collectionView?.mj_header.endRefreshing()
        })
    }
    
    @objc func loadMore(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
            self.collectionView?.mj_footer.endRefreshingWithNoMoreData()
        })
    }
}


// MARK: - UICollectionViewDataSource, UICollectionViewDelegate
extension YMLeaseSectionHeaderView: UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCellId, for: indexPath) as! YMLeaseNormalCell
        return cell
    }
}


// MARK: - YMTitleViewDelegate
extension YMLeaseSectionHeaderView: YMPageTitleViewDelegate {
    func classTitleView(_ titleView: YMPageTitleView, selectedIndex index: Int) {
        print("点击了:\(titles[index])")
    }
}

extension YMLeaseSectionHeaderView {
    class func nibView() -> YMLeaseSectionHeaderView{
        return Bundle.main.loadNibNamed("YMLeaseSectionHeaderView", owner: nil, options: nil)?.first as! YMLeaseSectionHeaderView
    }
}
